package com.msu.nava.activity;


import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.msu.nava.MainActivity;
import com.msu.nava.R;
import com.msu.nava.consts.URLs;
import com.msu.nava.modle.User;
import com.msu.nava.util.HttpUtil;
import com.msu.nava.util.MD5encode;

import org.w3c.dom.Text;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class LoginActivity extends BaseActivity {

  private static final String LOGIN_LOCALURL =URLs.LOGIN_REMOTEURL ;
  private static final String REGISTER_LOCALURL =URLs.REGISTER_REMOTEURL ;
  private EditText usernameEdt;
  private EditText passwordEdt;
  private EditText pwdAgEdt;
  private Button loginbtn;
  private Button regbtn;
  private ProgressDialog progressDialog;
  private FrameLayout frameLayout ;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    initView();
    setListeners();


  }

  private void initView() {
    usernameEdt = (EditText) findViewById(R.id.username);
    passwordEdt = (EditText) findViewById(R.id.password);
    pwdAgEdt = (EditText) findViewById(R.id.pwdag);
    loginbtn = (Button) findViewById(R.id.loginbtn);
    regbtn = (Button) findViewById(R.id.reg);
    frameLayout = (FrameLayout)findViewById(R.id.userpwdag_layout);
  }

  private void setListeners() {
    loginbtn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        login();
      }
    });
    regbtn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        regster();
      }
    });

  }

  private void login() {
    String usrname = usernameEdt.getText().toString();
    String password = passwordEdt.getText().toString();
    if (TextUtils.isEmpty(usrname)) {
      Toast.makeText(LoginActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
      return;
    }
    if (TextUtils.isEmpty(password)) {
      Toast.makeText(LoginActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
      return;
    }

    showProgressDialog();
    HttpUtil.loginRequest(LOGIN_LOCALURL, usrname, password, new Callback() {
      @Override
      public void onFailure(Call call, IOException e) {
        runOnUiThread(new Runnable() {
          @Override
          public void run() {
            closeProgressDialog();
            Toast.makeText(LoginActivity.this, "网络错误，请检查网络", Toast.LENGTH_SHORT).show();
          }
        });
      }

      @Override
      public void onResponse(Call call, Response response) throws IOException {
        String result = response.body().string();//获取返回结果
        if (result == null) {
          runOnUiThread(new Runnable() {
            @Override
            public void run() {
              closeProgressDialog();
              Toast.makeText(LoginActivity.this, "网络错误，请检查网络", Toast.LENGTH_SHORT).show();
            }
          });
        }
        if (result.equals("success")) {
          runOnUiThread(new Runnable() {
            @Override
            public void run() {
              closeProgressDialog();
              Toast.makeText(LoginActivity.this, "登录成功！", Toast.LENGTH_SHORT).show();
              intent2Activity(MainActivity.class);
            }
          });
        } else if (result.equals("notfound")) {
          runOnUiThread(new Runnable() {
            @Override
            public void run() {
              closeProgressDialog();
              Toast.makeText(LoginActivity.this, "用户不存在！请先注册", Toast.LENGTH_SHORT).show();
            }
          });

        } else if (result.equals("error")) {
          runOnUiThread(new Runnable() {
            @Override
            public void run() {
              closeProgressDialog();
              Toast.makeText(LoginActivity.this, "密码错误！请重试", Toast.LENGTH_SHORT).show();
            }
          });
        }else {
          runOnUiThread(new Runnable() {
            @Override
            public void run() {
              closeProgressDialog();
              Toast.makeText(LoginActivity.this, "内部错误！请重试", Toast.LENGTH_SHORT).show();
            }
          });
        }
      }
    });
  }

  private void regster() {
    //pwdAgEdt.setVisibility(View.VISIBLE);
    frameLayout.setVisibility(View.VISIBLE);
    String usrname = usernameEdt.getText().toString();
    String password = passwordEdt.getText().toString();
    String pwdag = pwdAgEdt.getText().toString();
    //显示再次输入密码的输入框

    if (TextUtils.isEmpty(usrname)) {
      Toast.makeText(LoginActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
      return;
    }
    if (TextUtils.isEmpty(password)) {
      Toast.makeText(LoginActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
      return;
    }
    if (TextUtils.isEmpty(pwdag)) {
      Toast.makeText(LoginActivity.this, "确认密码不能为空", Toast.LENGTH_SHORT).show();
      return;
    }

    if (password.length() < 6) {
      Toast.makeText(LoginActivity.this, "密码太短，请输入六位以上的密码", Toast.LENGTH_SHORT).show();
      return;
    }
    if (!TextUtils.equals(password, pwdag)) {
      Toast.makeText(LoginActivity.this, "两次密码不一致，请检查输入", Toast.LENGTH_SHORT).show();
      return;
    }
    showProgressDialog();
    //共享一个函数，只是请求的url不一样
    HttpUtil.loginRequest(REGISTER_LOCALURL, usrname, password, new Callback() {
      @Override
      public void onFailure(Call call, IOException e) {
        runOnUiThread(new Runnable() {
          @Override
          public void run() {
            closeProgressDialog();
            Toast.makeText(LoginActivity.this, "网络错误，请检查网络", Toast.LENGTH_SHORT).show();
          }
        });
      }

      @Override
      public void onResponse(Call call, Response response) throws IOException {
        String result = response.body().string();

        if (result.equals("success")) {
          runOnUiThread(new Runnable() {
            @Override
            public void run() {
              closeProgressDialog();
              Toast.makeText(LoginActivity.this, "注册成功！", Toast.LENGTH_SHORT).show();
              intent2Activity(MainActivity.class);
            }
          });
        } else if (result.equals("existed")) {
          closeProgressDialog();
          Toast.makeText(LoginActivity.this, "用户已存在！请登录", Toast.LENGTH_SHORT).show();
          return;
        } else {
          runOnUiThread(new Runnable() {
            @Override
            public void run() {
              closeProgressDialog();
              Toast.makeText(LoginActivity.this, "内部错误！请重试", Toast.LENGTH_SHORT).show();
              return;
            }
          });
        }

      }
    });


  }
  /**
   * 显示进度对话框
   */
  private void showProgressDialog() {
    if (progressDialog == null) {
      progressDialog = new ProgressDialog(LoginActivity.this);
      progressDialog.setMessage("正在加载...");
      progressDialog.setCanceledOnTouchOutside(false);
    }
    progressDialog.show();
  }

  /**
   * 关闭进度对话框
   */
  private void closeProgressDialog() {
    if (progressDialog != null) {
      progressDialog.dismiss();
    }
  }


}
